package com.sumainfor.pc.epc.controller.buyer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.ObjectUtil;
import com.sumainfor.pc.epc.service.SettlementManageService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * 结算单
 *
 * @Author PuaChen
 * @Create 2018-12-03 18:18
 */
@RestController
@RequestMapping("/settlement/manage")
@Log4j2
public class SettlementManageController {

    @Autowired
    private SettlementManageService slt;

    /**
     * 结算单主页列表查询显示
     * @param page
     * @param params
     * @return
     */
    @RequestMapping("/getSettlementList")
    public JsonResult getSettlementList(Page page, @RequestParam Map<String, Object> params) {
        return slt.getSettlementList(page, params);
    }


    /**
     * 查询获得结算单详情底部的列表数据
     *
     * @param params type 1 获取的是账单维度，2获取的是供应商的维度
     *               sourceId 结算单主表编号或者是供应商的编号
     *               state 供应商专用 筛选状态是否是已结算的
     * @return
     */
    @RequestMapping("/selectOrderInfos")
    public JsonResult selectOrderInfos(Page page, @RequestParam Map<String, Objects> params) {
        if (!FieldCheckUtil.isMapFieldNotNull(params, "type", "sourceId")) {
            return JsonResult.errorForEmpty();
        }
        return slt.selectOrderInfos(page, params);
    }

    /**
     * 申请打款
     *
     * @param params
     * @return
     */
    @PostMapping("/applyForMoney")
    public JsonResult applyForMoney(@RequestParam Map<String, Object> params) {
        if (!FieldCheckUtil.isMapFieldNotNull(params, "StaId", "SupId")) {
            return JsonResult.errorForEmpty();
        }
        return slt.applyForMoney(params);
    }

    /**
     * 获得查看详情的列表数据 账单明细
     *
     * @param params
     * @return
     */
    @RequestMapping("/selectOrderInfo")
    public JsonResult selectOrderInfo(@RequestParam Map<String, Objects> params) {
        if (!FieldCheckUtil.isMapFieldNotNull(params, "StaId", "SupId")) {
            return JsonResult.errorForEmpty();
        }
        return slt.selectOrderInfo(params);
    }

    /**
     * 查询申请打款记录列表
     * 必须参数 ： StaId
     *
     * @param page
     * @param params
     * @return
     */
    @RequestMapping("/selectApplyBillLog")
    public JsonResult selectApplyBillLog(Page page, @RequestParam Map<String, Object> params) {
        if (!FieldCheckUtil.isMapFieldNotNull(params, "StaId")) {
            return JsonResult.errorForEmpty();
        }
        return slt.selectApplyBillLog(page, params);
    }


    /**
     * 查询申请打款记录 通过供应商编号查询
     *
     * @param page
     * @param params
     * @return
     */
    @RequestMapping("/selectApplyBillLogForSupplier")
    public JsonResult selectApplyBillLogForSupplier(Page page, @RequestParam Map<String, Object> params) {
        String[] kes = {"SupId"};
        if (!FieldCheckUtil.isMapFieldNotNull(params, kes)) {
            return JsonResult.errorForEmpty();
        }
        return slt.selectApplyBillLogForSupplier(page, params);
    }

    /**
     * 返回 结算单账单类型 下拉框筛选条件
     *
     * @return
     */
    @RequestMapping("/getApplyTypeList")
    public JsonResult getApplyTypeList() {
        return slt.getApplyTypeList();
    }

    /**
     * 撤回一条 申请打款记录
     *
     * @param remId
     * @return
     */
    @PostMapping("/deleteApplyRemit")
    public JsonResult deleteApplyRemit(String remId) {
        if (StringUtils.isBlank(remId)) {
            return JsonResult.errorForEmpty();
        }
        return slt.deleteApplyRemit(remId);
    }

    /**
     * 预算校正
     *
     * @param stId     主结算单的编号
     * @param supId    供应商的编号
     * @param sumPrice 校正的价格
     * @return
     */
    @PostMapping("/budgetRevise")
    public JsonResult budgetRevise(String stId, String supId, String sumPrice) {
        if (StringUtils.isBlank(stId) || StringUtils.isBlank(sumPrice) || StringUtils.isBlank(supId)) {
            return JsonResult.errorForEmpty();
        }
        return slt.budgetRevise(stId, supId, ObjectUtil.convertBigDecimal(sumPrice));
    }
}
